﻿namespace USElections.Tiles
{
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media.Animation;
    using System.Windows.Threading;
    using Microsoft.Phone.Controls;
    using Telerik.Windows.Controls;

    public partial class TilesManager : UserControl
    {
        #region Fields
        private UserControl frontTile;
        private UserControl backTile;
        private DispatcherTimer timer;
        private bool showingFront = true;
        private Random random = new Random();
        private bool isLoaded = false;
        #endregion

        #region Constructor
        public TilesManager(UserControl frontTile, UserControl backTile, Uri navigateUri)
        {
            this.frontTile = frontTile;
            this.backTile = backTile;
            
            if (frontTile == null)
            {
                throw new ArgumentNullException();
            }

            InitializeComponent();

            if (navigateUri != null)
            {
                this.LayoutRoot.Tap += (sender, e) =>
                {
                    PhoneApplicationFrame paf = Application.Current.RootVisual as PhoneApplicationFrame;

                    if (paf != null)
                    {
                        paf.Navigate(navigateUri);
                    }
                };
            }

            this.Loaded += this.FirstLook_Loaded;

            if (this.backTile != null)
            {
                this.InitializeTransition();
            }
            else
            {
                this.transitionControl.Content = this.frontTile;
            }
        }
        #endregion

        #region Private methods
        private void FirstLook_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.backTile != null && !this.isLoaded)
            {
                this.isLoaded = true;
                this.timer = new DispatcherTimer();
                this.timer.Interval = TimeSpan.FromMilliseconds(this.random.Next(2000,5000));
                this.timer.Tick += this.timer_Tick;
                this.timer.Start();
            }
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            this.timer.Stop();
            this.ChangeContent();
        }

        private void ChangeContent()
        {
            if (this.showingFront && this.backTile != null)
            {
                try
                {
                    this.transitionControl.Content = this.backTile;
                    this.showingFront = false;
                }
                catch (Exception ex)
                {
                }
            }
            else
            {
                try
                {
                    this.transitionControl.Content = this.frontTile;
                    this.showingFront = true;
                }
                catch (Exception ex)
                {
                }
            }

            if (this.backTile != null)
            {
                this.timer.Interval = TimeSpan.FromMilliseconds(this.random.Next(2000, 5000));
                this.timer.Start();
            }
        }

        private void InitializeTransition()
        {
            Duration duration = new Duration(TimeSpan.FromMilliseconds(250));

            ExponentialEase easing = new ExponentialEase();
            easing.EasingMode = EasingMode.EaseInOut;
            easing.Exponent = 1;

            RadPlaneProjectionAnimation forwardInAnimation = new RadPlaneProjectionAnimation()
            {
                Axes = PerspectiveAnimationAxis.X,
                CenterX = 0.5,
                StartAngleX = -90,
                EndAngleX = 0,
                Duration = duration,
                Easing = easing
            };

            RadPlaneProjectionAnimation forwardOutAnimation = new RadPlaneProjectionAnimation()
            {
                Axes = PerspectiveAnimationAxis.X,
                CenterX = 0.5,
                StartAngleX = 0,
                EndAngleX = 90,
                Duration = duration,
                Easing = easing
            };

            this.transitionControl.Transition = new RadTurnstileTransition()
            {
                ForwardInAnimation = forwardInAnimation,
                ForwardOutAnimation = forwardOutAnimation
            };

            this.transitionControl.Content = frontTile;
        }
        #endregion
    }
}
